package cn.iocoder.yudao.module.dify.controller.admin.dify;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.dify.api.vo.resp.DifyChatFlowUploadResponse;
import cn.iocoder.yudao.module.dify.service.riskManagementReport.ChatFlowService;
import cn.iocoder.yudao.module.dify.service.riskreportrecords.RiskReportRecordsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

@RestController
@RequestMapping("/api/dify")
@RequiredArgsConstructor
@Tag(name = "管理后台 - dify风险管理报告")
public class DifyRiskManagementReportController {

    @Value("${spring.dify.key.chatflow-test}")
    private String chatflowApiKey;

    @Resource
    private ChatFlowService chatFlowService;

    @Resource
    private RiskReportRecordsService riskReportRecordsService;

    @PostMapping(value = "/riskManagementReport/uploadFile", produces = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "风险管理报告-上传文件")
    public CommonResult<DifyChatFlowUploadResponse> riskManagementReportUploadFile(@RequestParam("file") MultipartFile file) throws Exception {
        //上传文件到dify
        DifyChatFlowUploadResponse difyChatFlowUploadResponse = chatFlowService.chatFlowUploadFile(chatflowApiKey, file, getLoginUserId());
        return CommonResult.success(difyChatFlowUploadResponse);
    }

    @PostMapping(value = "/riskManagementReport/performTasks", produces = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "风险管理报告-执行任务")
    public CommonResult<Boolean> riskManagementReportPerformTasks(@RequestParam("id") Long id) throws Exception {
        Long riskReportRecordsCount = riskReportRecordsService.getRiskReportRecordsCount(getLoginUserId());
        if (riskReportRecordsCount > 0) {
            ErrorCode WORKFLOW_CODE_WORKING = new ErrorCode(1_040_011_002, "你有任务正在生成,请稍候");
            return CommonResult.error(ServiceExceptionUtil.exception(WORKFLOW_CODE_WORKING));
        }
        //提交任务
        Boolean b = chatFlowService.riskManagementReportPerformTasks(id, getLoginUserId(), chatflowApiKey);
        return CommonResult.success(b);
    }

    @PostMapping(value = "/riskManagementReport/retry", produces = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "风险管理报告-重试")
    @PreAuthorize("@ss.hasPermission('dify:risk-report-records:retry')")
    public CommonResult<Boolean> riskManagementReportRetry(@RequestParam("id") Long id) throws Exception {
        Long riskReportRecordsCount = riskReportRecordsService.getRiskReportRecordsCount(getLoginUserId());
        if (riskReportRecordsCount > 0) {
            ErrorCode WORKFLOW_CODE_WORKING = new ErrorCode(1_040_011_002, "你有任务正在生成,请稍候");
            return CommonResult.error(ServiceExceptionUtil.exception(WORKFLOW_CODE_WORKING));
        }
        riskReportRecordsService.updateRiskReportStatus(id, 0);
        Boolean b = chatFlowService.riskManagementReportRetry(id, getLoginUserId(), chatflowApiKey);
        return CommonResult.success(b);
    }

    @PostMapping(value = "/riskManagementReport/uploadReviewFile", produces = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "风险管理报告-上传审核后文件")
    public CommonResult<Boolean> riskManagementReportUploadReviewFile(@RequestParam("id") Long id, @RequestParam("file") MultipartFile file) throws Exception {
        return CommonResult.success(riskReportRecordsService.riskManagementReportUploadReviewFile(id, file));
    }
}
